﻿using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfance;
using B.S.BasicData.Read.Apilication.Common.Leave;
using B.S.BasicData.Read.DTOS;
using MediatR;

namespace B.S.BasicData.Read.Apilication.Handler.Leave
{
    public class GetLeaveCommandHandle:IRequestHandler<GetLeaveCommand, ApiPaimg<LeaveDto>>
    {
        private readonly IBaseRepository<LeaveModel> LeaveDal;
        private readonly IBaseRepository<DepartmentModel> DepatDal;
        public GetLeaveCommandHandle(IBaseRepository<LeaveModel> leaveDal, IBaseRepository<DepartmentModel> depatDal)
        {
            LeaveDal = leaveDal;
            DepatDal = depatDal;
        }

        

        public Task<ApiPaimg<LeaveDto>> Handle(GetLeaveCommand request, CancellationToken cancellationToken)
        {
            ApiPaimg<LeaveDto> result = new ApiPaimg<LeaveDto>();

            var leave = LeaveDal.GetAll().Where(x => x.IsDeleted == false);
            var deparment = DepatDal.GetAll();
            var isboosid = DepatDal.GetAll().Where(x => x.ParentId.Equals(request.UserId)).Select(s => s.DepartmentId).ToList();

            if (isboosid.Any())
            {
                leave = leave.Where(x => isboosid.Contains(x.DepartmentId));
            }
            else
            {
                // leave = leave.Where(x => x.UserId.Equals(request.UserId));
                leave = leave.Where(x => x.UserId.Equals(request.UserId));
            }


            if (!string.IsNullOrEmpty(request.leaveName))
            {
                leave = leave.Where(x => x.LeaveName.Contains(request.leaveName));
            }

            if (request.leaveTime != null)
            {
                leave = leave.Where(x => x.leaveTime >= request.leaveTime);
            }
            if (request.EndleaveTime != null)
            {
                DateTime time1 = Convert.ToDateTime(request.EndleaveTime);
                leave = leave.Where(x => x.EndleaveTime < time1.AddDays(1));
            }

            if (request.Sates != null)
            {
                leave = leave.Where(x => x.Sates == request.Sates);
            }


            var linq =( from a in leave
                       join b in deparment
                       on a.DepartmentId equals b.DepartmentId
                       select new LeaveDto()
                       {
                           leaveId = a.LeaveId,
                           leaveName = a.LeaveName,
                           leavecause = a.leavecause,
                           CreateTime = a.CreateTime,
                           EndleaveTime = a.EndleaveTime,
                           typeId = a.TypeId,
                           leaveTime = a.leaveTime,
                           cause = a.cause,
                           DepartmentId = a.DepartmentId,
                           DepartmentName = b.DepartmentName,
                           DepartmentManager = a.DepartmentManager,
                           IsDelId = a.IsDeleted,
                           Number = a.Number,
                           typeIds = a.typeIds,
                           Sates = a.Sates,
                       }).ToList();

            result.ToTaCount = linq.Count();
            result.ToTaPage = (int)Math.Ceiling(result.ToTaCount * 1.0 / request.PageSize);
            result.PageData = linq.OrderByDescending(x => x.leaveId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

            result.Message = "审批列表显示";
            return Task.FromResult(result);

        }



    }
}
